package demo02;

/**
 * 1、封装
 * 2、构造方法
 */
class Student {
    String id;//学生学号
    private String name;//学生姓名
    private int age;//学生年龄
    private int gender;// 性别 0 男 1 女

    //构造方法
    //多个同名构造方法(构造方法重载)
    public Student() {
    }

    public Student(String id) {
        this.id = id;
    }

    public Student(String id, String name) {
        this.id = id;
        this.name = name;
    }

    //封装一个方法
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    //封装 有利于对数据校验
    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        if (age < 0 || age > 150) {
            System.out.println("输入年龄错误");
        } else {
            this.age = age;
        }
    }

    //封装 有利于复杂功能的重复使用
    public String getGender() {
        if (gender == 0) {
            return "男";
        } else {
            return "女";

        }
    }


    public void setGender(String gender) {
        if (gender.equals("男")) {
            this.gender = 0;
        } else {
            this.gender = 1;
        }

    }

}

public class Demo2 {
    public static void main(String[] args) {
        Student stu1 = new Student("23304010599");
        System.out.println(stu1.id);//查看构造方法设置的学号
        stu1.id = "23304010599";
//        stu1.name = "猪小明";
        stu1.setName("猪小明");
        System.out.println(stu1.getName());
        stu1.setAge(180);
        stu1.setGender("女");
        System.out.println(stu1.getGender());
    }
}
